package com.finance.controller.user.finance;

import com.finance.common.Result;
import com.finance.pojo.others.ChangeMoney;
import com.finance.pojo.others.FlowOfFunds;
import com.finance.pojo.user.User;
import com.finance.pojo.user.UserChangeMoney;
import com.finance.service.user.finance.ChangeMoneyService;
import com.finance.service.user.userinfo.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;

@Controller
public class UserChangeMoneyController {
    @Autowired
    private ChangeMoneyService changeMoneyService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 投资某一零钱理财产品
     * 添加用户购买、投资记录
     * 添加资金的流水记录
     * @param userid
     * @param changeMoneyId
     * @param paymoney
     * @return
     */
    @RequestMapping("/user/buyChangeMoney")
    @ResponseBody
    public Result insertRecords(@RequestParam("userId") Integer userid, @RequestParam("changeMoneyId") Integer changeMoneyId,
                                @RequestParam("paymoney") BigDecimal paymoney){

        UserChangeMoney userChangeMoney = new UserChangeMoney();
        FlowOfFunds flowOfFunds = new FlowOfFunds();

        ChangeMoney changeMoney = changeMoneyService.selectById(changeMoneyId);
        //填写user_change_money记录
        userChangeMoney.setUserid(userid);
        userChangeMoney.setChangeid(changeMoneyId);
        userChangeMoney.setStarttime(new Date());
        userChangeMoney.setAveryield(changeMoney.getAnnualincome());
        BigDecimal param1 = changeMoney.getAnnualincome();
        BigDecimal param2 = param1.multiply(paymoney);
        //收益=投资*收益率
        userChangeMoney.setProfit(param2);
        userChangeMoney.setStatus(1);
        //填写资金流水记录
        flowOfFunds.setUserid(userid);
        flowOfFunds.setCreatetime(new Date());
        flowOfFunds.setFlowmoney(paymoney);
        flowOfFunds.setSource(changeMoney.getName());
        flowOfFunds.setType(1);
        flowOfFunds.setFunddesc("无");

        int result1 = changeMoneyService.insertUserChangeMoney(userChangeMoney);
        int result2 = changeMoneyService.insertFlowOfFunds(flowOfFunds);
        //System.out.println("----零钱投资----");
        if(result1 == 1&&result2 ==1){
            return Result.success();
        }else{
            return Result.fail();
        }
    }
    /**
     * 验证用户的支付密码
     * @param paypassword
     * @param userid
     * @return
     */
    @RequestMapping("/user/verifyPayPassword")
    @ResponseBody
    public Result verifyPayPassword(@RequestParam("paypassword") Integer paypassword, @RequestParam("userId") Integer userid){
        User user = userInfoService.selectUserById(userid);
        if(user.getPaypwd().equals(paypassword)){
            return Result.success();
        }else{
            return Result.fail();
        }
    }

}
